// MailNoter - A capture tool for emails

// Copyright (C) 2009 - MailNoter

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "StdAfx.h"
#include "Resource.h"
#include "OptionsDlg.h"
#include "InfoConfigDlg.h"
#include "Utils.h"
#include <CommCtrl.h>

COptionsDlg::COptionsDlg(HWND hParent)
	: regCaptureTextHK(_T("Software\\MailNoter\\captureTextHK"), DEFAULT_CAPTURE_HOTKEY)
	, regMailTo(_T("Software\\MailNoter\\mailto"))
	, regMailFrom(_T("Software\\MailNoter\\mailfrom"))
	, regStartWithWindows(_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\MailNoter"))
{
	m_hParent = hParent;
}

COptionsDlg::~COptionsDlg(void)
{
}

LRESULT COptionsDlg::DlgFunc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (uMsg)
	{
	case WM_INITDIALOG:
		{
			InitDialog(hwndDlg, IDI_MAILNOTER);
			// initialize the controls
			hotkeyCtrl.ConvertEditToHotKeyCtrl(hwndDlg, IDC_CAPTUREHK);
			hotkeyCtrl.SetHotKey((WPARAM)(DWORD)regCaptureTextHK);
			::SetDlgItemText(hwndDlg, IDC_MAILFROM, ((std::wstring)regMailFrom).c_str());
			::SetDlgItemText(hwndDlg, IDC_MAILTO, ((std::wstring)regMailTo).c_str());
			bool bStartWithWindows = !std::wstring(regStartWithWindows).empty();
			SendMessage(GetDlgItem(*this, IDC_AUTORUN), BM_SETCHECK, bStartWithWindows ? BST_CHECKED : BST_UNCHECKED, NULL);
		}
		return TRUE;
	case WM_COMMAND:
		return DoCommand(LOWORD(wParam), HIWORD(wParam));
	default:
		return FALSE;
	}
	return FALSE;
}

LRESULT COptionsDlg::DoCommand(int id, int /*msg*/)
{
	switch (id)
	{
	case IDOK:
		{
			bool bStartWithWindows = !!SendMessage(GetDlgItem(*this, IDC_AUTORUN), BM_GETCHECK, 0, NULL);
			if (bStartWithWindows)
			{
				TCHAR buf[MAX_PATH*4];
				GetModuleFileName(NULL, buf, MAX_PATH*4);
				std::wstring cmd = std::wstring(buf);
				regStartWithWindows = cmd;
			}
			else
				regStartWithWindows.removeValue();

			TCHAR buf[1024] = {0};

			regCaptureTextHK = hotkeyCtrl.GetHotKey();

			// try registering the hotkey
			WORD captureTextHK = (WORD)(DWORD)regCaptureTextHK;
			captureTextHK = CUtils::HotKeyControl2HotKey(captureTextHK);

			if (!RegisterHotKey(*this, CAPTURE_HOTKEY, HIBYTE(captureTextHK), LOBYTE(captureTextHK)))
			{
				TCHAR hkText[MAX_PATH];
				GetDlgItemText(*this, IDC_CAPTUREHK, hkText, MAX_PATH);
				TCHAR msg[2048] = {0};
				_stprintf_s(msg, 2048, _T("Failed to register the hotkey <%s>!\n\nThis can happen if another application has already registered the same hotkey.\nPlease choose another hotkey."), hkText);
				::MessageBox(*this, msg, _T("MailNoter"), MB_ICONERROR);
				return 1;
			}

			::GetDlgItemText(*this, IDC_MAILFROM, buf, 1024);
			regMailFrom = buf;
			::GetDlgItemText(*this, IDC_MAILTO, buf, 1024);
			regMailTo = buf;
		}
		// fall through
	case IDCANCEL:
		EndDialog(*this, id);
		break;
	case IDC_EDITINFOBAR:
		{
			CInfoConfigDlg dlg(*this);
			dlg.DoModal(hResource, IDD_INFOCONFIG, *this);
		}
		break;
	}
	return 1;
}

